﻿using Business.HouseManagement;
using Business.HouseManagement.Dto;
using Business.Models;
using Business.Permissions;
using Microsoft.AspNetCore.Authorization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;
using XCZ;

namespace Business.HousManagement
{
    [Authorize(BusinessPermissions.House.Default)]
    public class HouseAppService: BusinessWorkFlowAppService, IHouseAppService
    {
        private const string FormName = "House";
        private IRepository<House, Guid> _repository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="repository"></param>
        public HouseAppService(IRepository<House, Guid> repository)
        {
            _repository = repository;
        }

        #region 仓库基础的增删改查 
        /// <summary>
        /// 创建或更新仓库信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<HouseDto> CreateOrUpdate(CreateOrUpdateHouseDto input)
        {
            House result = null;
            if (input.Id.HasValue)
            {
                input.Id = GuidGenerator.Create();
                result = await _repository.InsertAsync(ObjectMapper.Map<CreateOrUpdateHouseDto, House>(input));

                // 创建工作流
                await WorkFlow.CreateWorkFlow(FormName, result);
            }
            else
            {
                var data = await _repository.GetAsync(input.Id.Value);
                result = await _repository.UpdateAsync(ObjectMapper.Map(input, data));

                //检查流程状态
                await WorkFlow.CheckFormStatus(input.Id.Value, result);
            }
            return ObjectMapper.Map<House, HouseDto>(result);
        }
        /// <summary>
        /// 删除仓库信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task Delete(List<Guid> ids)
        {
            foreach (var item in ids)
            {
                await _repository.DeleteAsync(item);
            }
        }
        /// <summary>
        /// 获取仓库信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<HouseDto> Get(Guid id)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 获取仓库列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<PagedResultDto<HouseDto>> GetAll(GetBookInputDto input)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}
